<?php
//error_reporting(0);
header("Content-Type:text/html;charset=utf-8");
class Opmysqli
{
	private $host;
	private $userName;
	private $password;					//登录密码
	private $dataBase;				//数据库名称
	private $conn = '';						//数据库链接资源
	private $result = '';					//dml数据库查询结果
	private $msg = '';						//返回结果,错误信息
	private $fields;						//返回字段
	private $fieldsNum = 0;					//返回结果集列数
	private $rowsNum = 0;					//返回结果集总行数
	private $filesArray = array();			//返回字段数组
	
	//连接数据库，初始化的

	function __construct($host='',$userName='',$password='',$dataBase='')
	{
		if ($host != '')
			$this->host = $host;
		if ($userName != '')
			$this->userName = $userName;
		if ($password != '')
			$this->password = $password;
		if ($dataBase != '')
			$this->dataBase = $dataBase;
		$this->init_conn();
	}
	
	//连接数据库
	private function init_conn()
	{
		$this->conn = new mysqli($this->host, $this->userName, $this->password, $this->dataBase);
		$this->conn->query("set names utf8");
	}

	//错误信息
	function msg_error()
	{
		if ($this->conn->connect_errno)
		{
			$this->msg = "数据库连接失败，".$this->conn->connect_error;
		}
		return $this->msg;
	}

	//执行多个insert、update、语句
	function mysqli_multi_dml($sqls)
	{
		if ($this->conn == "")
		{
			$this->init_conn();
		}
		$this->result = $this->conn->multi_query($sqls);
		if(!$this->result)
		{
			$this->result = $this->conn->error;
		}
		return $this->result;
	}

	//执行单条sql语句
	function sql_dml($sql)
	{	
		$res_d = array("error" => false);
		if ($this->conn->query($sql))
		{
			$res_d["error"] = true;
			$res_d["info"] = "删除成功！";
		}	
		else
		{
			$res_d["info"] = "删除数据失败！".$this->conn->error;
		}	
		return $res_d;
	}

	//取得结果
	function getRowsArray($sql)
	{
		$rowsArray = array();
		if($res = $this->conn->query($sql))
		{
			while ($row = $res->fetch_assoc())
			{
				$rowsArray[] = $row;   //rowsArray是一个二维数组
			}
			$res->free();
		}
		else
		{
			echo "<div class=\"well color_c\">";
			echo "数据库检索数据失败！".$this->conn->error;
			echo "</div>";
    		exit(0);
		}
		return $rowsArray;
	}

		//获得查询结果的行数
	function getRowsNum($sql)
	{
		if ($res = $this->conn->query($sql))
		{
			$this->rowsNum = $res->num_rows;  //$result->num_rows,结果集的行数
		}
		else
		{
			echo "<div class=\"well color_c\">";
			echo "数据库检索数据失败！".$this->conn->error;
			echo "</div>";
    		exit(0);
		}
		return $this->rowsNum;
	}
	
	/*
	function mysqli_multi_dql($sqls)
	{
		$i = 0;
		if ($this->conn->multi_query($sqls))
		{
			do
			{
				if ($result = $this->conn->store_result())
				{
					while ($row = $result->fetch_row())
					{
						$this->rowsArray[$i] = $row;
						$i++;
					}
					$result->free();
				}
				if (!$this->conn->more_results())
				{
					break;
				}
			
			}while($this->conn->next_result());
		}
		return $this->rowsArray;  //这是一个二维数组
	}
	

	
	//获取字段数
	function getFieldsNum($sql)
	{
		//先获取字段结果
		$this->mysqli_query_res($sql);
		//然后获取字段，字段数就是数据表的列数，从结果集中获取字段数
		$this->fieldsNum = @$this->result->field_count;
	}



	
	//更新、删除、添加记录数

	
	//获取对应的字段值
	function getFields($sql, $fields)
	{
		$this->mysqli_query_res($sql);
		if (mysqli_num_rows($this->result) > 0)
		{
			$tmpfld = mysqli_fetch_row($this->result);
			$this->fields = $tmpfld[$fields];
		}
		return $this->fields;
	}
	

	//释放结果集*/
	function close_res(){
		$this->msg = '';
		$this->fieldsNum = 0;
		$this->rowsNum = 0;
		$this->filesArray = '';
		$this->result = '';
	}
	
	//关闭数据库
	function close_conn(){
		$this->close_res();
		$this->conn->close();
		$this->conn = '';
	}

}



?>
